import { fileURLToPath, URL } from "node:url";

import { loadEnv, defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "node:path";

// https://vitejs.dev/config/
export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  console.log(env);
  return defineConfig({
    lintOnSave: false, //设置是否在开发环境下每次保存代码时都启用 eslint验证
    plugins: [vue()],
    base: "./",
    resolve: {
      // 路径别名
      alias: {
        "@": resolve(__dirname, "src"),
      },
    },
    server: {
      // 服务启动后自动在浏览器中打开
      open: true,
      port: env.VITE_PORT,
      proxy: {
        // 选项写法
        "/carbon-class": {
          target: env.VITE_API_URL,
          changeOrigin: true,
          // rewrite: (path) => path.replace(/^\/api/, ""),
          // headers: {
          //   referer: "https://www.sugarttt.top",
          //   origin: "/api",
          // },
        },
      },
    },
    // 打包配置
    build: {
      target: "modules",
      outDir: "dist",
      assetsDir: "static",
      // chunk最大限制警告改为1000
      chunkSizeWarningLimit: 1000,
      // 打包时不生成.map文件
      productionSourceMap: false,
      // 代码分块，以此减少每个chunk大小，
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes("node_modules")) {
              return id
                .toString()
                .split("node_modules/")[1]
                .split("/")[0]
                .toString();
            }
          },
        },
      },
    },
  });
};
